// 此处存放所有的请求链接，用于和后端通用逻辑接口对接，此业务与具体的业务无关
// 1、授权
// 3、场景值
// 使用方法：
// 引入方式 const API_COMMON = require("../../../utils/api_common")
// 调用方法 API_COMMON.activity();
const api_busi = require("./api_busi");
const app = getApp();
let API_COMMON = {
  createScene(opts = {}, afterFn) {
    let {activityId,path,type,sceneInfoExtend} = opts;
    api_busi.common.createScene({activityId,path,type,sceneInfoExtend},(res)=>{
      let data = res.data
      if (data.code === 0) {
        app.globalData.businessId = data.model.scene;
        afterFn();
      } else {
        wx.showModal({title: '提示',content: data.msg || data.message,showCancel: false});
      }
    },(fail)=>{
      console.log('网络错误，请重试！(#qqbjdzf)');
    });
  },

  parseScene(scene, afterFn, failFn) {
    api_busi.common.parseScene({scene},(res)=>{
      let data = res.data
      if (data.code === 0) {
        afterFn(data.model);
      } else {
        wx.showModal({title: '提示',content: data.msg || data.message,showCancel: false});
      }
    },(fail)=>{
      console.log('网络错误，请重试！(#qqbjdzf)');
      failFn();
    });
  },

  wxPayFunc: function (data,callbackFn) {
    let amount = Math.ceil(data.money / 0.98);
    api_busi.commonly.create_wx_pay_order({amount}, (res) => {
      if (res.data.code == 0) {
        let wxOrderData = res.data.model.wxOrderData;
        let submitData = {
          "appId": wxOrderData.appId,
          "nonceStr": wxOrderData.nonceStr,
          "paySign": wxOrderData.paySign,
          "signType": wxOrderData.signType,
          "timeStamp": wxOrderData.timeStamp,
          "package": wxOrderData.package
        };
        wx.requestPayment(Object.assign({}, submitData, {
          success: function (res) {
            wx.showToast({title: "支付成功",icon: 'success',duration: 2000});
            if(callbackFn && typeof callbackFn == "function"){
              callbackFn(res,data);
            }
          },
          fail: function (res) {
            wx.showToast({title: '已取消',duration: 2000});
          }
        }));
      }
    }, (fail) => {
      wx.showModal({content: "创建订单出错！"})
    });
  },

  ajaxFail(data){
    data = data || {};
    data.msg = data.msg || "网络延迟，请稍后重试！";
    if(data.type == 1){//显示toast类型
      data.time = data.time || 2200;
      wx.showToast({title: data.msg,icon: 'none',duration: data.time});
    }else{
      wx.showModal({title: '温馨提示',content: data.msg,showCancel: false});
    }
    return false;
  },

  navigateTo(data) {
    if (getCurrentPages().length < 5) {
      wx.navigateTo(data);
    } else {
      wx.redirectTo(data);
    }
  }
}

module.exports = API_COMMON;
